﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Resources;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using DMS.Library.Extensions;

namespace DMS.Library.Web.Mvc
{
    public static class HtmlHelperExtension
    {
        private static UrlHelper Url(this HtmlHelper html)
        {
            return ((WebViewPage)html.ViewDataContainer).Url;
        }

        public static MvcHtmlString Script(this HtmlHelper html, string fileName, bool autoResolveUrl = true)
        {
            if (autoResolveUrl)
                fileName = html.Url().Script(fileName);
            else
            {
                fileName += UrlHelperExtension.appVersion;
            }

            string htmlString = string.Format("<script type='text/javascript' src='{0}'></script>", fileName);

            return MvcHtmlString.Create(htmlString);
        }

        public static MvcHtmlString ScriptAjax(this HtmlHelper html, string relativePath)
        {
            var ajaxRoot = ConfigurationManager.AppSettings["AjaxRoot"];

            return html.Script(new Uri(new Uri(ajaxRoot), relativePath).ToString(), false);
        }


        public static MvcHtmlString Css(this HtmlHelper html, string fileName, bool autoResolveUrl = true)
        {
            if (autoResolveUrl)
                fileName = html.Url().Css(fileName);
            else
            {
                fileName += UrlHelperExtension.appVersion;
            }

            string htmlString = string.Format("<link rel='stylesheet' type='text/css' href='{0}' />", fileName);

            return MvcHtmlString.Create(htmlString);
        }

        public static MvcHtmlString Image(this HtmlHelper html, string fileName, object htmlAttributes = null)
        {
            TagBuilder tagBuilder = new TagBuilder("image");
            tagBuilder.MergeAttribute("src", html.Url().Image(fileName));
            tagBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));

            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, ResourceManager resourceManager, Expression<Func<TModel, TProperty>> expression, Type enumType, bool? createEmptyItem = null, string emptyItemValue = "", string emptyItemText = "", object htmlAttributes = null)
        {
            // get model value
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var modelValue = metadata.Model;

            // build item list
            IEnumerable<Enum> values = Enum.GetValues(enumType).Cast<Enum>();

            var items = new List<SelectListItem>();
            foreach (var value in values)
            {
                string text = resourceManager.GetEnumString(value);
                if (string.IsNullOrWhiteSpace(text))
                    text = value.ToString();
                items.Add(new SelectListItem
                {
                    Text = text,//Groupon.Resources.EnumResource.ResourceManager.GetEnumString(value),
                    Value = ((int)Enum.Parse(enumType, value.ToString())).ToString(),
                    Selected = modelValue != null && ((int)Enum.Parse(enumType, value.ToString())).ToString().Equals(modelValue.ToString())
                });
            }


            if (createEmptyItem == true)
            {
                items.Insert(0, new SelectListItem { Text = string.IsNullOrWhiteSpace(emptyItemText) ? @"--- Vui lòng chọn ---" : emptyItemText, Value = emptyItemValue });

            }


            return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
        }

    }
}